home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58189 / 58189.xpi / chrome / content / whitelist.js < prev    next >
Text File  |  2010-01-06  |  5KB  |  192 lines

  1. /*
  2.  * This class handles the functionality of the whitelist window
  3.  */
  4.  
  5. Components.utils.import("resource://csfiremodules/CsFireCommon.jsm");
  6.  
  7. CsFire.XulWhitelist = new function() {};
  8.  
  9.  
  10. /*
  11.  * This function enables/disables the ADD button when appropriate
  12.  */
  13. CsFire.XulWhitelist.updateAddButtonStatus = function() {
  14.    var addbtn = document.getElementById('addbtn');
  15.    var originbox = document.getElementById('urlboxorigin');
  16.    var destinationbox = document.getElementById('urlboxdestination');
  17.    addbtn.disabled = !(originbox.value.length > 0 && destinationbox.value.length > 0);
  18. }
  19.  
  20. /*
  21.  * This function enables/disables the DEL button when appropriate
  22.  */
  23. CsFire.XulWhitelist.updateDelButtonStatus = function() {
  24.     var list = document.getElementById('whitelist');
  25.     var rules = document.getElementById('listcontents');
  26.        var delbtn = document.getElementById('delbtn');
  27.        var disabled = true;
  28.        
  29.        for (var i=rules.childNodes.length-1 ; i>=0 && disabled; i--) {
  30.         if (list.view.selection.isSelected(i)) {
  31.             disabled = false;
  32.         }
  33.     }
  34.    delbtn.disabled = disabled;
  35. }
  36.  
  37. /*
  38.  * Updates the "enabled" status of entries in the whitelists.
  39.  */
  40. CsFire.XulWhitelist.updateEntries = function() {
  41.     var entries = CsFire.Whitelist.getList();
  42.     var list = document.getElementById('whitelist');
  43.     var rules = document.getElementById('listcontents');
  44.     for (var i=rules.childNodes.length-1 ; i>=0 ; i--) {
  45.         var node = rules.childNodes[i];
  46.         var origin = node.firstChild.childNodes[0].getAttribute('label');
  47.         var destination = node.firstChild.childNodes[1].getAttribute('label');
  48.         var value = node.firstChild.childNodes[2].getAttribute('value');
  49.         
  50.         for(var j = 0; j < entries.length; j++) {
  51.             var entry = entries[j];
  52.             if(entry && entry.origin == origin && entry.destination == destination) {
  53.                 if(value === "true") {
  54.                     if(!entry.enabled) {
  55.                         CsFire.Whitelist.updateEntryStatus(entry);
  56.                     }
  57.                 }
  58.                 else {
  59.                     if(entry.enabled) {
  60.                         CsFire.Whitelist.updateEntryStatus(entry);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. /*
  69.  * Initializes the components of the window
  70.  */
  71. CsFire.XulWhitelist.init = function() {
  72.     this.clearList();    
  73.     this.loadList();
  74. }
  75.  
  76. /*
  77.  * Removes all items from the domain list (only in the view, not the controller)
  78.  */
  79. CsFire.XulWhitelist.clearList = function() {
  80.     var rules = document.getElementById('listcontents');
  81.     while (rules.hasChildNodes()) rules.removeChild(rules.firstChild);
  82. }
  83.  
  84. /*
  85.  * Loads items from controller in the view (populates domain list)
  86.  */
  87. CsFire.XulWhitelist.loadList = function() {
  88.     var list = CsFire.Whitelist.getList();
  89.     for(var i = 0; i < list.length; i++) {
  90.         var item = list[i];
  91.         if(item) {
  92.             this.addListItem(item);
  93.         }
  94.     }
  95. }
  96.  
  97. /*
  98.  * Adds an item to the visual list of domains
  99.  */
  100. CsFire.XulWhitelist.addListItem = function(entry) {
  101.     var rules = document.getElementById('listcontents');
  102.     var item = document.createElement('treeitem');
  103.     var row = document.createElement('treerow');
  104.     
  105.     // Add the domain
  106.     var c1 = document.createElement('treecell');
  107.     c1.setAttribute('label', entry.origin);
  108.     row.appendChild(c1);
  109.     
  110.     // Add the domain
  111.     var c2 = document.createElement('treecell');
  112.     c2.setAttribute('label', entry.destination);
  113.     row.appendChild(c2);
  114.     
  115.     // Add the enabled field
  116.     var c3 = document.createElement('treecell');
  117.     c3.setAttribute('value', entry.enabled);
  118.     row.appendChild(c3);
  119.     
  120.     // Add entry to list
  121.     item.appendChild(row);
  122.     rules.appendChild(item);
  123. }
  124.  
  125. /*
  126.  * Removes an item from the visual list of domains
  127.  */
  128. CsFire.XulWhitelist.removeListItem = function(node) {
  129.     var rules = document.getElementById('listcontents');
  130.     rules.removeChild(node);
  131. }
  132.  
  133. /*
  134.  * Adds a URL to the whitelist (controller and view)
  135.  */
  136. CsFire.XulWhitelist.addEntry = function() {
  137.     var origin = document.getElementById('urlboxorigin');
  138.     var destination = document.getElementById('urlboxdestination');
  139.     var listItem = CsFire.Whitelist.addEntry(origin.value, destination.value);
  140.     if("error" in listItem) {
  141.         if("errororigin" in listItem) {
  142.             var style = origin.style;
  143.             style['color'] = "red";
  144.         }
  145.         if("errordestination" in listItem) {
  146.             var style = destination.style;
  147.             style['color'] = "red";
  148.         }
  149.     }
  150.     else {
  151.         this.addListItem(listItem);
  152.  
  153.         origin.value="";
  154.         var style = origin.style;
  155.         style['color'] = null;
  156.  
  157.         destination.value="";
  158.         var style = destination.style;
  159.         style['color'] = null;
  160.     }
  161.     this.updateAddButtonStatus();
  162. }
  163.  
  164. /*
  165.  * Removes an entry from the whitelist (controller and view)
  166.  */
  167. CsFire.XulWhitelist.removeEntry = function() {
  168.     var toRemove = [];
  169.  
  170.     var list = document.getElementById('whitelist');
  171.     var rules = document.getElementById('listcontents');
  172.     for (var i=rules.childNodes.length-1 ; i>=0 ; i--) {
  173.         if (list.view.selection.isSelected(i)) {
  174.             var node = rules.childNodes[i];
  175.             var origin = node.firstChild.childNodes[0].getAttribute('label');
  176.             var destination = node.firstChild.childNodes[1].getAttribute('label');
  177.             if(CsFire.Whitelist.removeEntry(origin, destination)) {
  178.                 toRemove.push(node);
  179.             }
  180.         }
  181.     }
  182.     
  183.     /*
  184.      * Delete all entries here (otherwise, you're modifying the list you're 
  185.      * looping over
  186.      */
  187.     for(var i = 0; i < toRemove.length; i++) {
  188.         this.removeListItem(toRemove[i]);
  189.     }
  190.     
  191. }
  192.